package com.hcx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcx.reggie.dto.SetmealDto;
import com.hcx.reggie.exception.CustomException;
import com.hcx.reggie.pojo.Category;
import com.hcx.reggie.pojo.Setmeal;
import com.hcx.reggie.pojo.SetmealDish;
import com.hcx.reggie.service.CategoryService;
import com.hcx.reggie.service.SetMealDishService;
import com.hcx.reggie.service.SetmealService;
import com.hcx.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.lang.invoke.CallSite;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 套餐管理
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("setmeal")
public class SetMealController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetMealDishService setMealDishService;

    /**
     * 主页面查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage  = new Page<>();
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (name != null){
            dishLambdaQueryWrapper.like(Setmeal::getName,name);
        }
        dishLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,dishLambdaQueryWrapper);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map((item)->{
            Category categoryServiceById = categoryService.getById(item.getCategoryId());
            String categoryServiceByIdName = categoryServiceById.getName();
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            setmealDto.setCategoryName(categoryServiceByIdName);
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @Transactional
    @DeleteMapping()
    public R<String> delete(@RequestParam List<Long> ids){

        for (Long id : ids) {
            Setmeal setmealServiceById = setmealService.getById(id);
            if (setmealServiceById.getStatus()==1){
                throw new CustomException("未停售的套餐不能删除");
            }
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            setMealDishService.remove(setmealDishLambdaQueryWrapper);
        }
        setmealService.removeBatchByIds(ids);
        return R.success("删除成功");

    }

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @Transactional
    @PostMapping()
    public R<String> addSetMeal(@RequestBody SetmealDto setmealDto) {
        log.info("{}",setmealDto.getId());
        setmealService.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            String setMealName = setmealDto.getName();
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getName,setMealName);
            Setmeal setmeal = setmealService.getOne(setmealLambdaQueryWrapper);
            Long setmealId = setmeal.getId();
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setmealDishes);
        return R.success("增加成功");
    }

    /**
     * 修改套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable int status ,@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getId,id);
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmealService.update(setmeal, setmealLambdaQueryWrapper);
        }
        return R.success("");
    }

    /**
     * 修改套餐信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> updatePage(@PathVariable Long id){
        Setmeal setmealServiceById = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmealServiceById,setmealDto);
        Long setmealServiceByIdId = setmealServiceById.getId();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealServiceByIdId);
        List<SetmealDish> dishes = setMealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(dishes);
        return R.success(setmealDto);
    }
    /**
     * 以DTo的方式返回套餐的信息
     */
    @GetMapping("list")
    public R<List<SetmealDto>> list(Long categoryId ,int status){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (categoryId != null){
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        }
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        List<SetmealDto> setmealDtoList = setmealList.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            LambdaQueryWrapper<SetmealDish> LambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper.eq(SetmealDish::getSetmealId,item.getId());
            List<SetmealDish> setmealDishes = setMealDishService.list(LambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishes);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(setmealDtoList);
    }




}
